//
//  FavoriteViewController.m
//  ios_news
//
//  Created by Han on 14-7-22.
//  Copyright (c) 2014年 ucoder. All rights reserved.
//

#import "ActViewController.h"
#import "Activity.h"
#import "ActivityCell.h"

@implementation ActViewController




- (void)viewDidLoad
{
    [super viewDidLoad];
    
    self.navigationController.navigationBarHidden = NO;
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationController.navigationBar.barTintColor = MAIN_COLOR;
    self.navigationController.navigationBar.translucent = NO;  // 半透明
    
    self.navigationItem.title = @"活动";
    self.navigationController.navigationBar.titleTextAttributes = [NSDictionary dictionaryWithObject:[UIColor whiteColor] forKey:NSForegroundColorAttributeName];
    
    [self addButton];
    
    mData = [NSMutableArray arrayWithCapacity:30];
    
    
    float mTableViewHeight = GLOBLE_HEIGHT - 64; // nav 64 tabar 49
    
    mTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 320, mTableViewHeight)];
    [mTableView setSeparatorStyle: UITableViewCellSeparatorStyleNone];
    
    mTableView.delegate = self;
    mTableView.dataSource = self;
    
    [self.view addSubview:mTableView];
    
    [self loadData];
}


- (void) loadData
{
    NSString *url = [[NSString alloc] initWithFormat:@"%@", API_ACTIVITY];
    
    [Activity getDataWithBlock:^(NSArray *data, NSError *error) {
        if (!error) {
            //NSLog(@"%@", data);
            [mData addObjectsFromArray:data];

            [mTableView reloadData];
            
        } else {
            NSLog(@"%@", error);
        }
    } :url];
    
    
}


- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    
    //NSMutableArray *currentData = [mData objectAtIndex:mCurrentPage];
    
    //NSLog(@"currentData count = %i", [currentData count]);
    
    static NSString *CellIdentifier = @"ActivityCell";
    ActivityCell *cell = (ActivityCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[ActivityCell alloc] initWithStyle:UITableViewCellStyleDefault
                                   reuseIdentifier:CellIdentifier];
    }
    
    cell.act = [mData objectAtIndex:(NSUInteger)indexPath.row];
    
    return cell;
    
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return [mData count];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{

    return 120;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    Activity *act = [mData objectAtIndex:(NSUInteger)indexPath.row];
    
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:act.url]];
}



//添加按钮
-(void)addButton
{
    UIButton *leftButton = [UIButton buttonWithType:UIButtonTypeCustom];
    leftButton.exclusiveTouch = YES;
    leftButton.frame = CGRectMake(0, 0, 44, 44);
    //button.backgroundColor = [UIColor redColor];
    
    [leftButton setImage:[UIImage imageNamed:@"back"] forState:UIControlStateNormal];
    leftButton.imageEdgeInsets = UIEdgeInsetsMake(0, -30, 0, 0);
    
    [leftButton addTarget:self action:@selector(backButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *leftButtonItem = [[UIBarButtonItem alloc] initWithCustomView:leftButton];
    self.navigationItem.leftBarButtonItem = leftButtonItem;
    
}

-(void)backButtonClicked:(id)sender
{
    [self.navigationController popViewControllerAnimated:YES];
}




@end
